package com.imay.live.activity.family;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.imay.live.R;
import com.imay.live.activity.BaseRequestActivity;
import com.imay.live.activity.family.controller.BaseFamilyController;
import com.imay.live.activity.family.controller.FamilyRankController;
import com.imay.live.activity.family.controller.FamilySettingController;
import com.imay.live.manager.FamilyAccountManager;
import com.imay.live.model.FamilyModel;
import com.imay.live.model.FamilyUserModel;
import com.imay.live.proto.ErrorResult;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;

/**
 * Created by Administrator on 2017/9/18 0018.
 */

public class FamilySettingActivity extends BaseRequestActivity implements View.OnClickListener {
    private FamilySettingController controller;

    private String requestFamilyUserCommand;

    private FamilyModel family;

    private int count = 5;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, FamilySettingActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_family_setting;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setTitle(R.string.str_family_setting_title);
        controller = (FamilySettingController) FamilySettingController.Builder.create(this, FamilySettingController.class).build();
        if (FamilyAccountManager.getInstance().isHasFamilyInfo()) {
            family = FamilyAccountManager.getInstance().getFamilyUserInfo().getFamily();
            controller.setFamilyInfoData(family);
            loadFamilyUsers();
        }
    }

    private void loadFamilyUsers() {
        requestFamilyUserCommand = ProtoRequestHelper.task(ProtoHttpFactory.getFamilyUsers(family.getId(), 0, count)).setTag(this.hashCode()).request(this);
    }

    @Override
    public void onRequestSuccess(String command, String data) {
        super.onRequestSuccess(command, data);
        if (command.equals(requestFamilyUserCommand)) {
            Result<FamilyUserModel> result = JSON.parseObject(data, new TypeReference<Result<FamilyUserModel>>() {
            }.getType());
            controller.setUserData(result.data);
        }
    }

    @Override
    public void onRequestError(String command, int code, String message) {
        super.onRequestError(command, code, message);
        ErrorResult.toastError(this, code);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_family:
                FamilyHomeActivity.startActivitySingleTop(FamilySettingActivity.this, FamilyHomeActivity.ME);
                break;
            case R.id.fiv_family_users:
                FamilyPersonActivity.startActivity(FamilySettingActivity.this, family.getId(), family.getDesignation(), FamilyPersonActivity.ME);
                break;
            case R.id.ll_family_permission:
                FamilyPermissionActivity.startActivity(FamilySettingActivity.this);
                break;
        }
    }
}
